﻿/**
 * TableConfig.java	1.0 2007 五月 29 15:18:14,创建
 */

package org.in4j.search.index;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author 杨帆
 * @version 1.0 2007 五月 29 15:18:14,创建
 */
public class TableConfig
{
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(TableConfig.class);

	private String name;
	private float boost;
	private String where;
	private Set<String> excludeFields = new HashSet<String>();
	private Map<String, FieldConfig> fieldConfigs = new HashMap<String, FieldConfig>();

	public void setName(String name)
	{
		this.name = name;
	}

	public void setBoost(float boost)
	{
		this.boost = boost;
	}

	public void addExcludeField(String field)
	{
		excludeFields.add(field.toLowerCase());
	}

	public void setWhere(String where)
	{
		this.where = where;
	}

	public Set<String> getExcludeFields()
	{
		return excludeFields;
	}

	public float getBoost()
	{
		return boost;
	}

	public String getName()
	{
		return name;
	}

	public String getWhere()
	{
		return where;
	}

	public Set<String> getColumns(DatabaseMetaData metaData) throws SQLException
	{
		log.debug("getColumns for table " + getName());
		Set<String> columns = new HashSet<String>();
		ResultSet rs = null;
		try
		{
			rs = metaData.getColumns(null, null, this.getName().toUpperCase(), "%");
			while(rs.next())
			{
				String columnName = rs.getString("COLUMN_NAME").toLowerCase();
				log.debug("TABLE_NAME==>" + rs.getString("TABLE_NAME"));
				log.debug("columnName==>" + columnName);
				if(this.excludeFields.contains(columnName)) continue;
				columns.add(columnName);
			}
		}
		finally
		{
			if(rs != null)
				rs.close();
		}
		return columns;
	}

	public void addFieldConfig(FieldConfig fieldConfig)
	{
		fieldConfigs.put(fieldConfig.getName().toLowerCase(), fieldConfig);
	}

	public FieldConfig getFieldConfig(String fieldName)
	{
		return fieldConfigs.get(fieldName.toLowerCase());
	}

}
